﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ChurchData.Business;

namespace ChurchData
{
    public partial class PeopleKeywordsUC : UserControl
    {
        #region Fields / Properties
        private int PeoplePK { get; set; }

        #endregion Fields / Properties

        #region Constructor
        public PeopleKeywordsUC()
        {
            InitializeComponent();
            if (UserInformation.ViewOnlyInd)
            {
                btnAdd.Enabled = false;
                btnRemove.Enabled = false;
            }
        }

        #endregion Constructor

        #region Populate Methods
        public void LoadPersonInfo(int peoplePK)
        {
            this.PeoplePK = peoplePK;
            this.RefreshGrid();
            this.PopulateKeywordCombo();
        }

        public void PopulateKeywordCombo()
        {
            List<GenericLKTableResults> lstKeywords = new LKKeywordBO().GetActiveRecords();
            cboKeywords.DataSource = lstKeywords;
            cboKeywords.DisplayMember = "Description";
            cboKeywords.ValueMember = "Pk";
        }

        public void RefreshGrid()
        {
            PeopleKeywordBO pkeyBO = new PeopleKeywordBO();
            IEnumerable<PeopleKeyword> lstPKey = pkeyBO.GetRecordsByPeoplePK(PeoplePK);

            this.grdKeywords.DataSource = null;
            DataTable dt = new DataTable();
            dt.Columns.Add("PK", typeof(System.Int32));
            dt.Columns.Add("Keyword", typeof(System.String));
            dt.Columns.Add("LKKeywordFK", typeof(System.Int32));

            foreach (PeopleKeyword pKey in lstPKey)
            {
                dt.Rows.Add(new object[] { pKey.PeopleKeywordPK, pKey.LKKeyword.DescriptionText, pKey.LKKeywordFK });
            }

            this.grdKeywords.DataSource = dt;
            this.grdKeywords.Columns["PK"].Visible = false;
            this.grdKeywords.Columns["LKKeywordFK"].Visible = false;
        }

        #endregion Populate Methods

        #region Validation Methods
        private bool ValidateAgainstDuplicate()
        {
            int lkKeyWordFK = Convert.ToInt32(cboKeywords.SelectedValue);

            DataTable dt = (DataTable)grdKeywords.DataSource;

            DataRow[] drs = dt.Select("LKKeywordFK = " + lkKeyWordFK);

            if (drs.Length > 0)
            {
                MessageBox.Show("That keyword is already selected for this person.");
                return false;
            }
            else
                return true;
        }

        #endregion Validation Methods

        #region Control Events
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (cboKeywords.SelectedIndex >= 0)
            {
                if (ValidateAgainstDuplicate())
                {
                    PeopleKeywordBO pkBO = new PeopleKeywordBO();
                    pkBO.AddPeopleKeyword(this.PeoplePK, Convert.ToInt32(cboKeywords.SelectedValue));
                    this.RefreshGrid();
                }
            }
            else
            {
                MessageBox.Show("Please select a keyword to add");
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (grdKeywords.SelectedRows.Count == 0)
            {
                MessageBox.Show("Please select a keyword to remove");
                return;
            }

            if (DialogResult.Yes == MessageBox.Show("Are you sure?", "Confirm Delete", MessageBoxButtons.YesNo))
            {
                PeopleKeywordBO bo = new PeopleKeywordBO();
                bo.DeletePeopleKeyword((int)grdKeywords.SelectedRows[0].Cells["PK"].Value);

                this.RefreshGrid();
            }
        }

        #endregion Control Events
    }
}
